package com.adtech.adms.commons.web.intercepter;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.adtech.adms.Constants;
import com.adtech.adms.SessionKey;
import com.adtech.adms.commons.util.BeanLoader;
import com.adtech.adms.system.SystemConstants.Function;
import com.adtech.adms.system.SystemConstants.Statistic;
import com.adtech.core.dao.query.QueryParams;
import com.adtech.core.web.action.BaseAction;
import com.opensymphony.xwork2.ActionInvocation;

/**
 * 
 * 
 * @author Dormin 
 * @since 2011-5-17 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class AfterGridInterceptor extends BaseInterceptor {

	private static final long serialVersionUID = -6342795821580321655L;
	private static final Logger log = Logger.getLogger(BeforeFilterInterceptor.class);

	@Override
	@SuppressWarnings("unchecked")
	public String intercept(ActionInvocation ai) throws Exception {
		String result = ai.invoke();

		if (ai.getAction() instanceof BaseAction) {
			Map<String, Object> session = super.getSession(ai);
			Map<String, Object> statusMap = (Map<String, Object>) session.get(SessionKey.QUERY_STATUS);
			if (statusMap == null) {
				statusMap = new HashMap<String, Object>();
			}
			BaseAction action = (BaseAction) ai.getAction();
			String key = action.getClass().getName();
			
			QueryParams saveParams = (QueryParams) statusMap.get(key);
			if(saveParams==null){
				saveParams=new QueryParams();
			}
			saveParams.setPager(action.getPager());
			saveParams.setSorter(action.getSorter());
			saveParams.setFilter(action.getFilterMap());
			
			log.debug("put Params is " + key + "  " +  saveParams);
			statusMap.put(key, saveParams);
			session.put(SessionKey.QUERY_STATUS, statusMap);
		}
		return result;
	}

}
